package com.youmu.maven.emall.Util;

import java.sql.Connection;
import java.util.Map;
import java.util.Properties;

import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

@Deprecated
@Intercepts({ @Signature(type = StatementHandler.class, args = {
		Connection.class, Integer.class }, method = "prepare") })
public class PaginationInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		// TODO Auto-generated method stub
		System.err.println(this);
		Object target = invocation.getTarget();
		// BoundSql sql = (BoundSql) ReflectUtils.get(target, "boundSql");
		// Map params = (Map) sql.getParameterObject();
		// ReflectUtils.set(sql, "sql", "select * from sys_t_user");

		if (target instanceof RoutingStatementHandler) {
			StatementHandler sh = (StatementHandler) ReflectUtils.get(target,
					"delegate");
			BoundSql sql = (BoundSql) ReflectUtils.get(sh, "boundSql");
			Map params = (Map) sql.getParameterObject();
			params.put("page", 1);
			// ReflectUtils.set(sql, "sql", "select * from sys_t_user");
		}
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		// TODO Auto-generated method stub
		if (target instanceof RoutingStatementHandler) {
			return Plugin.wrap(target, this);
		} else {
			return target;
		}
	}

	@Override
	public void setProperties(Properties properties) {
		// TODO Auto-generated method stub

	}

}
